﻿Imports System.Data
Imports System.Data.SqlClient
Imports Datos
Imports Entidad

Public Class DocumentoClientePickeadorDatos

    Sub eliminarPedidoPickeador(ByVal codPedido As String, ByVal codPick As String, tipoDOC As String)
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "UPDATE Documento_Cliente_Pickeador " &
                              "SET DCP_Activo = '0' " &
                              "WHERE DCI_Codigo = (SELECT TOP (1) DCI_Codigo " &
                              "FROM Documento_Cliente_Pickeador " &
                              "WHERE DCI_Codigo = @dciCodigo " &
                              "AND TDOC_Codigo = @tipo " &
                              "ORDER BY DCP_Codigo DESC ) AND " &
                              "PICK_Codigo = (SELECT TOP (1) PICK_Codigo " &
                              "FROM Documento_Cliente_Pickeador " &
                              "WHERE PICK_Codigo = @pickCodigo AND " &
                              "DCI_Codigo = @dciCodigo " &
                              "AND TDOC_Codigo = @tipo " &
                              "ORDER BY DCP_Codigo DESC)"

        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@dciCodigo", codPedido)
            .Parameters.AddWithValue("@pickCodigo", codPick)
            .Parameters.AddWithValue("@tipo", tipoDOC)
            .ExecuteNonQuery()
        End With
        coneccion.Close()
    End Sub

    Function obtenerCodigo(ByVal codPedido As String, ByVal codPick As String, tipoDOC As String) As Integer
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "SELECT TOP 1 DCP_Codigo " &
                              "FROM Documento_Cliente_Pickeador " &
                              "WHERE DCI_Codigo = '" & codPedido & "' " &
                              "AND PICK_Codigo = '" & codPick & "' " &
                              "AND TDOC_Codigo = '" & tipoDOC & "' " &
                              "ORDER BY DCP_Codigo DESC"

        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)
        coneccion.Close()

        Return CInt(ds.Tables(0).Rows(0)("DCP_Codigo").ToString.Trim)
    End Function

End Class
